package servlet;


import jdbc.StudentHomeworkJdbc;
import model.Homework;
import model.Student;
import model.Student_homework;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ts")
public class TeacherServlet extends HttpServlet {

    private static String account = null;
    private static List<Homework> homeworkList;
    private static List<Student> studentList;

    protected List<Homework> get_student_homework_list(){
        return null;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        for(Cookie cookie: cookies){
            if(cookie.getName().equals("account")){
                account = cookie.getValue();
                System.out.println("student-homework-servlet:  "+ account);
            }
        }
        if(account.equals("1111")){        //教师登录
            homeworkList = StudentHomeworkJdbc.get_all_homework();
            studentList = StudentHomeworkJdbc.get_all_student();
            List<Student_homework> all_homework = StudentHomeworkJdbc.select_all();
            if(req.getParameter("target")!=null){
                String target = req.getParameter("target");
                System.out.println("teacherServlet target: "+target);
                Cookie cookie = new Cookie("target", target);
                resp.addCookie(cookie);
                switch (target){
                    case "0": req.getRequestDispatcher("teacher_index.jsp").forward(req, resp);
                    case "1": req.getRequestDispatcher("teacher_add_homework.jsp").forward(req, resp);
                    case "2": req.getRequestDispatcher("teacher_add_student.jsp").forward(req, resp);
                    case "3": req.setAttribute("homework_list", all_homework);
                                req.getRequestDispatcher("teacher_check_homework.jsp").forward(req, resp);
                }
            }
            req.getRequestDispatcher("teacher_index.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        for(Cookie cookie: cookies){
            if(cookie.getName().equals("target") && cookie.getValue().equals("1")){       //添加作业
                String homework_title = req.getParameter("title");
                byte  b[ ]=homework_title.getBytes("ISO-8859-1");
                homework_title = new String(b);
                int id = homeworkList.size()+1;
                String homework_id = id+"";
                StudentHomeworkJdbc.add_homework(homework_title, homework_id);
                cookie.setValue("0");
                req.getRequestDispatcher("teacher_index.jsp").forward(req, resp);
            }
            if(cookie.getName().equals("target") && cookie.getValue().equals("2")){        //添加学生
                String stu_name = req.getParameter("name");
                String stu_age = req.getParameter("age");
                String stu_tel = req.getParameter("tel");
                int id = studentList.size()+1;
                String student_id = id+"";
                StudentHomeworkJdbc.add_student(student_id, stu_name, stu_age, stu_tel);
                cookie.setValue("0");
                req.getRequestDispatcher("teacher_index.jsp").forward(req, resp);
            }
        }
    }
}
